//package com.wiiss.business.aspect;
//
//import com.wiiss.common.utils.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.slf4j.MDC;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
///**
// * 基于slf4j的MDC工具实现链路追踪
// * 在日志中打印missionId
// */
//@Order(0) // order数字越小的越先执行，越容易被覆盖（同心圆原则）
//@Aspect
//@Component
//@Slf4j
//public class MissionMDCAspect {
//
//    /**
//     * logback.xml文件读取的key值
//     */
//    private final static String MISSION_ID = "missionId";
//    @Autowired
//    private IMissionInfoService missionInfoService;
//
//
//    /**
//     * agv回调
//     */
//    @Around("execution (* com.wiiss.message.controller.*.*(..))")
//    public Object fillByTaskCode(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        String missionKey = "taskCode";
//        Object result;
//        try {
//            String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();
//            String functionName = proceedingJoinPoint.getSignature().getName();
//            log.info("MDC执行:" + className + "." + functionName);
//            //从请求参数中获取missionId
//            Object[] args = proceedingJoinPoint.getArgs();
//            for (Object arg : args) {
//                String taskCode = StringUtils.getBusinessNoFromArg(arg, missionKey);
//                if (StringUtils.isNotEmpty(taskCode)) {
//                    MissionInfo missionInfo = missionInfoService.selectMissionInfoById(taskCode);
//                    if (missionInfo != null) {
//                        log.info("从" + missionKey + "中获取到的mcs任务id:" + missionInfo.getId());
//                        MDC.put(MISSION_ID, missionInfo.getId());
//                        break;
//                    }
//                }
//            }
//            // 执行实际的方法
//            result= proceedingJoinPoint.proceed();
//        } finally {
//            MDC.remove(MISSION_ID);
//        }
//        return result;
//    }
//
//    /**
//     * 用户审批完成
//     */
//    @Around("execution (* com.wiiss.bpmn.controller.process.ProcessController.complete(..))")
//    public Object fillByInstanceId(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        String missionKey = "instanceId";
//        Object result;
//        try {
//            String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();
//            String functionName = proceedingJoinPoint.getSignature().getName();
//            log.info("MDC执行:" + className + "." + functionName);
//            //从请求参数中获取instanceId
//            Object[] args = proceedingJoinPoint.getArgs();
//            String instanceId = (String) args[1];
//            if (StringUtils.isNotEmpty(instanceId)) {
//                MissionInfo missionInfo = missionInfoService.selectMissionInfoByInstanceId(instanceId);
//                if (missionInfo != null) {
//                    log.info("从" + missionKey + "中获取到的mcs任务id:" + missionInfo.getId());
//                    MDC.put(MISSION_ID, missionInfo.getId());
//                }
//            }
//            // 执行实际的方法
//           result =  proceedingJoinPoint.proceed();
//        } finally {
//            MDC.remove(MISSION_ID);
//        }
//        return result;
//    }
//}
